package logic

import (
	"context"
	"github.com/pkg/errors"
	"yangming-college/app/usercenter/cmd/v1/rpc/internal/svc"
	"yangming-college/app/usercenter/cmd/v1/rpc/pb"

	"github.com/zeromicro/go-zero/core/logx"
)

type GetUserRoleLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGetUserRoleLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GetUserRoleLogic {
	return &GetUserRoleLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GetUserRoleLogic) GetUserRole(in *pb.UserInfoRequest) (*pb.UserRoleId, error) {
	var roleId int64
	err := l.svcCtx.DB.Table("accounts").Select("role_id").Where("account_id = ?", in.Id).Find(&roleId).Error
	if err != nil {
		return nil, errors.Wrapf(err, "查找用户角色")
	}
	return &pb.UserRoleId{
		RoleId: roleId,
	}, nil
}
